<?php

require_once __DIR__."/../constants.php";
require_once VENDOR_DIR."autoload.php";

use Predis\Client;
use Sunra\PhpSimple\HtmlDomParser;
$redis = new Client('tcp://127.0.0.1:6379');

const HASH_CONTENT_LIST = "hash:content:list";

for ($cursor = 0; $cursor >=0; ) {
    $result = $redis->hscan(HASH_CONTENT_LIST, $cursor, ['MATCH'=>'*','COUNT'=>10]);
    foreach ($result[1] as $cmd => $content) {
        echo $cmd."\n";
        $contentArray = json_decode($content, true);
        $dom =  HtmlDomParser::str_get_html( $contentArray['content'],true,true,DEFAULT_TARGET_CHARSET,false);
        //去掉content-index
        if(!empty($dom->find("#content-index", 0))) {
            $dom->find("#content-index", 0)->outertext = "";
        }
        if(!empty($imgList = $dom->find('img'))) {
            foreach ($imgList as $img) {
                $src = $img->src;
                $imageFileName = basename($src);
                $img->outertext = "<a href='{$src}' data-lightbox='{$imageFileName}'>{$img->outertext}</a>";
            }
        }
        $contentArray['content'] = $dom->root->outertext();
        $redis->hset(HASH_CONTENT_LIST, $cmd, json_encode($contentArray));
    }
    $cursor = $result[0];
    if($cursor == 0) {
        break;
    }
}